package com.jianyun.jyzs.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.base.MVPBaseActivity;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.presenter.UpdataEmailPresenter;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.RegularUtil;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.view.iview.IUpdateEmailView;
import com.jrmf360.rylib.common.util.KeyboardUtil;
import com.jrmf360.rylib.common.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yangshijun on 2018/8/1.
 */

public class UpdataEmailActivity extends MVPBaseActivity<IUpdateEmailView, UpdataEmailPresenter> implements IUpdateEmailView, View.OnClickListener {


    @BindView(R.id.et_email)
    EditText etEmail;

    private TextView optionsTextView;
    private String emailOld;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_update_email);
        ButterKnife.bind(this);
        emailOld = getIntent().getStringExtra(SysConstant.msg_key_string1);
        etEmail.setText(emailOld);
    }

    @NonNull
    @Override
    public UpdataEmailPresenter createPresenter() {
        return new UpdataEmailPresenter();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoding() {

    }

    @Override
    public void onSuccess() {
        ToastUtil.showToast(this, "修改邮箱成功！");
        finish();
    }

    @Override
    public void onFailed() {
        ToastUtil.showToast(this, "修改邮箱失败！");
    }

    @Override
    public void onException(String msg) {
        ToastUtil.showToast(this, msg);
    }


    @Override
    public void onCreateActionBar(ActionBar actionBar) {
        super.onCreateActionBar(actionBar);
        final View actionBarView = actionBar.setActionBar(R.layout.rce_actionbar_option_text);
        ImageButton backImgBtn = (ImageButton) actionBarView.findViewById(R.id.imgbtn_custom_nav_back);
        backImgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(-1);
                finish();
            }
        });
        TextView titleTextView = (TextView) actionBarView.findViewById(R.id.tv_custom_nav_title);
        titleTextView.setText(R.string.rce_change_email_title);
        optionsTextView = (TextView) actionBarView.findViewById(R.id.tv_custom_nav_option);
        optionsTextView.setText(R.string.rce_OK);
        setOptionsTextEnable(true);
        optionsTextView.setOnClickListener(this);
    }

    private void setOptionsTextEnable(boolean enable) {
        optionsTextView.setClickable(enable);
        optionsTextView.setTextColor(enable ? getResources().getColor(R.color.color_primary) :
                getResources().getColor(R.color.color_button_blue_disabled));
    }

    @Override
    public void onClick(View v) {
        //修改邮箱
        String newEmail = etEmail.getText().toString().trim();
        if (TextUtils.isEmpty(newEmail)) {
            ToastUtil.showToast(this, "邮箱不能为空");
            return;
        }
        if (emailOld.equals(newEmail)) {
            ToastUtil.showToast(this, "邮箱没有修改");
            return;
        }
        if (!RegularUtil.isEmailNO(newEmail)) {
            ToastUtil.showToast(this, "请输入正确的邮箱");
            return;
        }
        KeyboardUtil.hideKeyboard(this);
        String userId = LoginCache.getInstance().getUserInfo().getUserId();
        String enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        getPresenter().updataEmaile(enterpriseCode, userId, newEmail);
    }
}
